$(document).ready(function() {
    // set up the news accordion on the lower page
    $("#deaccordian").accordion({
        header: "h4" ,
        event: "mouseover",
        icons: false,
        autoHeight: false,
        navigation: true
    });
    $("li a:contains(Admin)").addClass('current');
    $("table#boardCompare").hide();
    $("div#users-contain").hide();
    $("select#category").change(get_complaints);
    $("div#ajaxload").hide();
    //  $("p.gear a").mouseover(function(){alert($(this).html());});
    $("#mainContent input:submit").button();
     $("input:button").button();
    $("#add_action").click(add_action);
   // $(".complete").click(complete);
});
var appid;
function show_table()
{
    $("#boardCompare tbody tr:even").addClass("even");
    $("#boardCompare tbody tr:odd").addClass("odd");
    $("#boardCompare tbody tr").click(highlight);
    $("div#ajaxload").hide();
    $("table#boardCompare").fadeIn('slow');
}
function show_action(){
    $("#boardCompare tbody tr:even").addClass("even");
    $("#boardCompare tbody tr:odd").addClass("odd");
    $("#actionstart tbody tr").click(highlight);
    $("table#actionstart").fadeIn('slow');
}
function get_complaints(){
    
    var category = $("select#category option:selected").text();

    if(category!="- Select One -"){
        //  alert(category);
        $("div#users-contain").hide();
        $("table#boardCompare").hide();
        $("div#ajaxload").show();

        data="category="+category+"&status=Undertaken";
        $.ajax({
            type: 'POST',
            url: base_url() + 'sde/get_complaints_ajax',
            data: data,
            datatype: "json",
            success : function (result) {
                var data=$.parseJSON(result);
                var html="";
                for(i=0;i<data.length;i++)
                {
                    html+="<tr>";
                    html+= "<td>"+data[i].appid+"</td>";
                    html+= "<td>"+data[i].applydate+"</td>";
                    html+= "<td>"+data[i].title+"</td>";
                    html+= "</tr>";
                }
                // alert(html);
                $("table#boardCompare tbody").html(html);
                show_table();

                
            }

        });
    }

}

function highlight(evt)
{
    $("div#users-contain").hide();
    $("div#ajaxload").show();
     appid=this.cells[0].innerHTML;
    data="appid="+appid;

    $.ajax({
        type: 'POST',
        url: base_url() + 'sde/get_action',
        data: data,
        datatype: "json",
        success : function (result) {
            var data=$.parseJSON(result);
            var html="";
            for(i=0;i<data.length;i++)
            {
                html+="<tr>";
                html+= "<td>"+data[i].id+"</td>";
                html+= "<td>"+data[i].description+"</td>";
                html+= "<td>"+data[i].startdate+"</td>";
                if(data[i].enddate!=null)
                    html+= "<td>"+data[i].enddate+"</td>";
                else
                    html+= "<td id="+data[i].id+">"+'<input type="button" name='+data[i].id+ ' value="Complete"  class="complete"  />'+"</td>";
                html+= "</tr>";
            }
          
            var hidden='<input type="hidden" name="id" value='+appid+" />";
           $("div#users-contain form").prepend(hidden);
           $("div#users-contain form div").prepend(hidden);

                        // alert(html);
            $("div#users-contain tbody").html(html);
            $("div#users-contain :button.complete").click(complete);
            $("div#ajaxload").hide();
            $("div#users-contain").fadeIn('slow');
            $("input:addaction").button();
            //$("div#users-contain input#solved").change(get_complaints_solved);
        }
    });

 }
  function complete(evt)
    {

        id=this.name;
        data="id="+id;

        $.ajax({
            type: 'POST',
            data:data,
            url: base_url() + 'sde/complain_complete_ajax',
            datatype: "html",
            success : function (result) {
                $("td#"+id).html(result);

            }
        });

    }
    function add_action(){
       var desc = $("#desc").val();
       var data2 = "appid="+appid+"&desc="+desc;
       
        $.ajax({
            type: 'POST',
            data:data2,
            url: base_url() + 'sde/add_action_ajax',
            datatype: "json",
            success : function (result) {
                var data=$.parseJSON(result);
               // alert(data);
                var html="";
                
                
                html+="<tr>";
                html+= "<td>"+data.id+"</td>";
                html+= "<td>"+data.description+"</td>";
                html+= "<td>"+data.startdate+"</td>";
                
                 html+= "<td id="+data.id+">"+'<input type="button" name='+data.id+ ' value="Complete"  class="complete"  />'+"</td>";
                html+= "</tr>";
                $("div#users-contain tbody").append(html);
                $("div#users-contain :button.complete").click(complete);

            }
        });
    }
